package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.SysCombo;
import com.ruoyi.system.domain.SysDishes;
import com.ruoyi.system.domain.SysEndOrders;
import com.ruoyi.system.mapper.SysMainComboMapper;
import com.ruoyi.system.mapper.SysTotalDishesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysOrdersMapper;
import com.ruoyi.system.domain.SysOrders;
import com.ruoyi.system.service.ISysOrdersService;

/**
 * 订单详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-12
 */
@Service
public class SysOrdersServiceImpl implements ISysOrdersService
{
    @Autowired
    private SysOrdersMapper sysOrdersMapper;
    @Autowired
    private SysTotalDishesMapper sysTotalDishesMapper;
    @Autowired
    private SysMainComboMapper sysMainComboMapper;
    /**
     * 查询订单详情
     * 
     * @param sysOrdersId 订单详情主键
     * @return 订单详情
     */
    @Override
    public SysOrders selectSysOrdersBySysOrdersId(Long sysOrdersId)
    {
        return sysOrdersMapper.selectSysOrdersBySysOrdersId(sysOrdersId);
    }

    /**
     * 查询订单详情列表
     * 
     * @param sysOrders 订单详情
     * @return 订单详情
     */
    @Override
    public List<SysEndOrders> selectSysOrdersList(SysOrders sysOrders)
    {
        List<SysEndOrders> endOrders = new ArrayList<>();
        List<SysOrders> orders = sysOrdersMapper.selectSysOrdersList(sysOrders);
        orders.stream().forEach(order -> {
            SysEndOrders endOrder = new SysEndOrders(order.getSysOrdersId(),order.getUserId(),order.getTableId(),null,null,order.getOrdersPrice(),
                    order.getOrdersPriceStatus(),order.getOrdersCreatTime(),order.getOrdersUpdateTime(),order.getOrdersStatus());
            String dishesCode = order.getDishesCode();
            List<SysDishes> sysDishes = new ArrayList<>();
            for (String dishesIdAndQuantity : dishesCode.split("\\+")) {

                String[] split = dishesIdAndQuantity.split("\\*");
                String dishesId = split[0];
                SysDishes dishes = sysTotalDishesMapper.selectSysDishesByDishesId(Long.valueOf(dishesId));
                String dishesQuantity = split[1];
                dishes.setDishesQuantity(dishesQuantity);
                sysDishes.add(dishes);
            }
            endOrder.setDishesList(sysDishes);
            String comboCode = order.getComboCode();
            List<SysCombo> sysCombos = new ArrayList<>();
            for (String comboDishesIdAndQuantity : comboCode.split("\\+")) {

                String[] split = comboDishesIdAndQuantity.split("\\*");
                String comboDishesId = split[0];
                SysCombo combos = sysMainComboMapper.selectSysComboByComboDishesId(Long.valueOf(comboDishesId));
                String comboQuantity = split[1];
                combos.setComboQuantity(comboQuantity);
                sysCombos.add(combos);
            }
            List<SysEndOrders> sysEndCombos = new ArrayList();
            endOrder.setComboList(sysCombos);
            endOrders.add(endOrder);
        });
        return endOrders;
    }

    /**
     * 新增订单详情
     * 
     * @param sysOrders 订单详情
     * @return 结果
     */
    @Override
    public int insertSysOrders(SysOrders sysOrders)
    {
        return sysOrdersMapper.insertSysOrders(sysOrders);
    }

    /**
     * 修改订单详情
     * 
     * @param sysOrders 订单详情
     * @return 结果
     */
    @Override
    public int updateSysOrders(SysOrders sysOrders)
    {
        return sysOrdersMapper.updateSysOrders(sysOrders);
    }

    /**
     * 批量删除订单详情
     * 
     * @param sysOrdersIds 需要删除的订单详情主键
     * @return 结果
     */
    @Override
    public int deleteSysOrdersBySysOrdersIds(Long[] sysOrdersIds)
    {
        return sysOrdersMapper.deleteSysOrdersBySysOrdersIds(sysOrdersIds);
    }

    /**
     * 删除订单详情信息
     * 
     * @param sysOrdersId 订单详情主键
     * @return 结果
     */
    @Override
    public int deleteSysOrdersBySysOrdersId(Long sysOrdersId)
    {
        return sysOrdersMapper.deleteSysOrdersBySysOrdersId(sysOrdersId);
    }

    @Override
    public List<SysOrders> getHisOrdersByUserId(Long userId) {
        return sysOrdersMapper.getHisOrdersByUserId(userId);
    }
}
